#!/bin/sh

# Slackware build script for pommed

# Copyright 2007  Alan Hicks (alan@lizella.net)
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
# EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

PRGNAM=pommed
VERSION=1.26
ARCH=${ARCH:-i486}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=${TMP}/package-${PRGNAM}
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e 

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.gz
cd $PRGNAM-$VERSION
chown -R root:root .
chmod -R u+w,go+r-w,a-s .

# Fix a bad assumption in the pommed Makefile
# If pciutils is rebuilt to include the pkgconfig file, this can be omitted,
# but at the time of this edit (20090729), it's not present in 13.0rc1
sed "s%@LIBDIR@%/usr/lib${LIBDIRSUFFIX}%" \
  $CWD/patches/pommed-1.26-link_libpci_a.diff | patch -p1 

# Use our CFLAGS instead of assuming -O2 only
patch -p1 < $CWD/patches/pommed-1.26-use_custom_cflags.diff

make OPT="$SLKCFLAGS"

# The Makefile doesn't have an "install" target, so we'll do it manually

# Install pommed (system daemon)
mkdir -p $PKG/usr/sbin $PKG/usr/share/pommed $PKG/etc
cp -a pommed/pommed $PKG/usr/sbin
cp -a pommed/data/* $PKG/usr/share/pommed
cat pommed.conf.mactel > $PKG/etc/pommed.conf.new

# Install gpomme (gtk client)
mkdir -p $PKG/usr/bin $PKG/usr/share/gpomme/themes $PKG/usr/share/applications
cp -a gpomme/gpomme $PKG/usr/bin
cp -a gpomme/themes/* $PKG/usr/share/gpomme/themes
cat gpomme/gpomme.desktop > $PKG/usr/share/applications/gpomme.desktop
mkdir -p $PKG/usr/share/icons/hicolor/scalable/apps
cp -a icons/gpomme.svg $PKG/usr/share/icons/hicolor/scalable/apps
for i in 16 22 24 32 36 48 64 72 96 128 192 ; do 
  mkdir -p $PKG/usr/share/icons/hicolor/${i}x${i}/apps
  cp -a icons/gpomme_${i}x${i}.png \
    $PKG/usr/share/icons/hicolor/${i}x${i}/apps/gpomme.png
done

# Install wmpomme (windowmaker client)
mkdir -p $PKG/usr/bin
cp -a wmpomme/wmpomme $PKG/usr/bin
mkdir -p $PKG/usr/share/pixmaps
cat icons/gpomme_192x192.xpm > $PKG/usr/share/pixmaps/gpomme.xpm

# Install dbus config file
mkdir -p $PKG/etc/dbus-1/system.d
cat dbus-policy.conf > $PKG/etc/dbus-1/system.d/pommed.conf.new

# Install init script
mkdir -p $PKG/etc/rc.d
cat $CWD/rc.pommed > $PKG/etc/rc.d/rc.pommed.new
chmod 0755 $PKG/etc/rc.d/rc.pommed.new

# Install manpages
mkdir -p $PKG/usr/man/man1
cat pommed.1 > $PKG/usr/man/man1/pommed.1
gzip -9 $PKG/usr/man/man1/pommed.1

# Install docs
mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a  AUTHORS COPYING ChangeLog INSTALL README TODO \
  $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/README.SLACKWARE > $PKG/usr/doc/$PRGNAM-$VERSION/README.SLACKWARE
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-tgz}
